package com.fj.netty.chat.server.service.impl;

import com.fj.netty.chat.server.service.Session;
import io.netty.channel.Channel;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author fj
 * @date 2024/1/12 14:02
 */
public class MapSessionImpl implements Session {
    private static final ConcurrentHashMap<String,Channel> usernameMap = new ConcurrentHashMap<>();
    private static final ConcurrentHashMap<Channel,String> channelMap = new ConcurrentHashMap<>();
    @Override
    public void bind(Channel channel, String username) {
        usernameMap.put(username, channel);
        channelMap.put(channel, username);
    }

    @Override
    public String unBind(Channel channel) {
        String username = channelMap.remove(channel);
        if (username != null){
            usernameMap.remove(username);
        }
        return username;
    }

    @Override
    public Channel getChannel(String username) {
        return usernameMap.get(username);
    }

    @Override
    public String getUsername(Channel channel) {
        return channelMap.get(channel);
    }
}
